package com.Drools.CIPS.ruler;

import com.Drools.CIPS.util.MapIN;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.*;

public class JS_Ruler {

    public static boolean excuteJava(String classPath, String methodName, Map<String, Object> msgMap){

        try {
            //读取java文件
            Class<?> aClass = Class.forName(classPath);

            // 获取方法参数类型
            Class<?>[] parameterTypes = {Map.class};

            //调用指定方法
            Method method = aClass.getMethod(methodName, parameterTypes);

            Object obj = aClass.getDeclaredConstructor().newInstance();

            //执行方法
            boolean invoke = (boolean) method.invoke(obj,msgMap);

            return invoke;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args) {
        Map<String,Object> m = new HashMap<>();
        m.put("MT_To_MXField72NewCodeWords",getMT_To_MXField72NewCodeWords());
//        String s = "MT_To_MXField72NewCodeWords";//执行的方法
        String s = "test11";//执行的方法
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "a",
//                s,
//                ((List) m.get(s)).get(0).toString());
                "/INTA/ds8dasd\n//666\n/awd/awdas\n//ads\n/adsdadawfawfa/5ad4a5");

        System.out.println(mt_to_mxPartyAccount);
        LinkedHashMap map = JSONObject.parseObject(mt_to_mxPartyAccount.toString(),LinkedHashMap.class);
        System.out.println(map);
//        System.out.println(((Map)map.get("c")).get("d"));
//        HashMap<String,Object> jsonMap = new ObjectMapper().readValue(mt_to_mxPartyAccount, HashMap.class);
//        System.out.println(jsonMap);
//        Map m1 = (Map)mt_to_mxPartyAccount;
//        Map m2 = (Map)m1.get("Identification");
//        Map m3 = (Map)m2.get("Other");
//        System.out.println(m3.get("Identification"));

    }

    public static List<Object> getMT_To_MXField72NewCodeWords(){
        List<Object> o = new ArrayList<>();
        o.add("/se/fslkfs\n/INTA/ds8dasd\n//adaefaewfae\n//asfas\n5aa");
        //

        return o;
    }

    public static Object excuteJs(String classPath, String methodName, Object msg){

        boolean res = false;

        //取得脚本引擎的管理器Manager
        ScriptEngineManager scriptManager= new ScriptEngineManager();

        //根据名称取得js的脚本引擎
        ScriptEngine engine = scriptManager.getEngineByName("javascript");

        try {
            //读js文件
            FileReader fileReader = new FileReader(classPath);

            //执行文件
            engine.eval(fileReader);

            if(engine instanceof Invocable){

                Invocable invoke = (Invocable)engine;

                //执行校验方法
                Object result= invoke.invokeFunction(methodName, msg);

                return result;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;

    }
    public static boolean excuteJs(String classPath, String methodName, Map<String,Object> msg){

        if(true) return false;

        boolean res = false;

        //取得脚本引擎的管理器Manager
        ScriptEngineManager scriptManager= new ScriptEngineManager();

        //根据名称取得js的脚本引擎
        ScriptEngine engine = scriptManager.getEngineByName("javascript");

        try {
            //读js文件
            FileReader fileReader = new FileReader(classPath);

            //执行文件
            engine.eval(fileReader);

            if(engine instanceof Invocable){

                Invocable invoke = (Invocable)engine;

                //执行校验方法
                boolean result= (boolean) invoke.invokeFunction(methodName, msg);

                return result;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;

    }

    public boolean method1(Map<String,Object> msgMap){
//        Object o = MapIN.GetValue(msgMap, "Document.FIToFICstmrCdtTrf.GrpHdr.MsgId");
//        if(o==null){
//            o="0";
//        }
//        return o.toString().equals("400");
        return false;

    }
}
